#include "slist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>


/* 头信息结构体的创建 */
int slist_create(slist_t **s, int size, ft1 destroy)
{
    /* 参数检查 */
    if (NULL == s || NULL == destroy || size <= 0)
    {
        perror("argument lawless...");
        goto err0;
    } /* end of if (NULL == s || NULL == destroy || size <= 0) */

    /* 申请空间 */
    *s = malloc(sizeof(slist_t));
    if (NULL == *s)
    {
        perror("*s malloc failed...");
        goto err0;
    } /* end of if (NULL == *s) */
    bzero(*s, sizeof(slist_t));

    /* 结构体初始化 */
    (*s)->count = 0;
    (*s)->size = size;
    (*s)->destroy = destroy;
    (*s)->p = NULL;



    return 0;
err0:
    return -1;
}


/* 节点创建 */
static node_t *__node_create(slist_t *s, void *data)
{
    /* 变量定义 */
    node_t *tmp = NULL;


    /* 参数检查 */
    if (NULL == s || NULL == data)
    {
        perror("argument lawless...");
        goto err0;
    } /* end of if (NULL == s || NULL == data) */

    /* 申请节点空间 */
    tmp = malloc(sizeof(node_t));
    if (NULL == tmp)
    {
        perror("node malloc failed...");
        goto err0;
    } /* end of if (NULL == tmp) */
    bzero(tmp, sizeof(node_t));

    /* 申请变量空间 */
    tmp->data = malloc(s->size);
    if (NULL == tmp->data)
    {
        perror("data malloc failed...");
        goto err1;
    } /* end of if (NULL == tmp->data) */
    bzero(tmp->data, s->size);

    /* 节点初始化 */
    tmp->next = NULL;
    memcpy(tmp->data, data, s->size);

    /* 节点个数+1 */
    s->count++;


    return tmp;
err1:
    free(tmp);
    tmp = NULL;
err0:
    return NULL;
}

/* 链表尾部插入 */
int slist_append(slist_t *s, void *data)
{
    /* 变量定义 */
    node_t *tmp = NULL;
    node_t *new_node = NULL;

    /* 参数检查 */
    if (NULL == s || NULL == data)
    {
        perror("argument lawless...");
        goto err0;
    } /* end of if (NULL == s || NULL == data) */

    /* 创建节点空间 */
    new_node = __node_create(s, data);

    /* 尾插 */
    tmp = s->p;
    if (NULL == tmp)
    {
        s->p = new_node;
    }
    else
    {
        while (tmp->next != NULL)
        {
            tmp = tmp->next;
        }
        tmp->next = new_node;
    }


    return 0;
err0:
    return -1;
}


/* 链表的遍历 */
int slist_traverse(slist_t *s, ft1 print)
{
    /* 变量定义 */
    node_t *tmp = NULL;

    /* 参数检查 */
    if (NULL == s || NULL == print)
    {
        perror("argument lawless...");
        goto err0;
    } /* end of if (NULL == s || NULL == print) */

    /* 遍历 */
    tmp = s->p;
    while (tmp != NULL)
    {
        print(tmp);
        tmp = tmp->next;
    } /* end of  while (tmp != NULL) */
    printf("\n");
    return 0;
err0:
    return -1;
}

/* 链表的销毁 */
int slist_destroy(slist_t *s)
{
    /* 变量定义 */
    node_t *tmp = NULL;
    node_t *save = NULL;

    /* 参数检查 */
    if (NULL == s)
    {
        perror("argument lawless...");
        goto err0;
    } /* end of if (NULL == s) */

    /* 销毁 */
    tmp = s->p;
    while (tmp != NULL)
    {
        /* 保存 */
        save = tmp->next;

        /* 销毁 */
        s->destroy(tmp);
        s->count--;

        /* 指向 */
        tmp = save;
    }

    /* 指向为空 */
    s->p = NULL;


    return 0;
err0:
    return -1;
}